package com.clz.latte.ec.main.Index;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.View;

import com.clz.latte.ec.R;
import com.clz.latte.ui.recycler.RgbValue;

/**
 * Created by czl on 2018/3/28.
 */
public class TranslucentBehavior extends CoordinatorLayout.Behavior<Toolbar> {
    //离顶部的距离
    private int mDistanceY = 0;
    //颜色变化的速度
    private static final int SHOW_SPEED = 3;
    //定义变化的颜色
    private static final RgbValue RGB_VALUE = RgbValue.create(255, 124, 2);

    public TranslucentBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, Toolbar child, View dependency) {
        return dependency.getId() == R.id.rel_goods;
    }

    @Override
    public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull Toolbar child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
        return true;
    }

    @Override
    public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull Toolbar child, @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type);
        //增加滑动距离
        mDistanceY += dyConsumed;
        //toolbar的高度
        final int toolbarHeight = child.getBottom();
        //当滑动时候，并且距离小于toolbar的高度
        if (mDistanceY <= toolbarHeight) {
            final float scale = (float) mDistanceY / toolbarHeight;
            final float alpha = scale * 255;
            child.setBackgroundColor(Color.argb((int) alpha, RGB_VALUE.red(), RGB_VALUE.green(), RGB_VALUE.blue()));
        }else {
            child.setBackgroundColor(Color.rgb(RGB_VALUE.red(), RGB_VALUE.green(), RGB_VALUE.blue()));
        }
    }
}
